package com.zkzx.operation.api.controller.driver;

import com.zkzx.common.company.model.CompanyDriverVO;
import com.zkzx.common.driver.command.DriverCommand;
import com.zkzx.common.driver.model.DriverListVO;
import com.zkzx.common.driver.model.DriverVO;
import com.zkzx.common.driver.param.DriverFilterParam;
import com.zkzx.common.global.Global;
import com.zkzx.common.truck.model.TruckVO;
import com.zkzx.common.vo.PageResult;
import com.zkzx.common.vo.RequestFilterParams;
import com.zkzx.operation.api.client.company.CompanyClient;
import com.zkzx.operation.api.client.driver.DriverClient;
import com.zkzx.operation.api.client.truck.TruckClient;
import com.zkzx.operation.api.controller.BaseController;
import com.zkzx.util.json.JSONResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * 北京中科智兴物联科技有限公司 @版权所有
 *
 * @Author: zhaoyihe@zkraising.com
 * @Date: 2021/05/08/17:24
 * @Description:
 */
@RestController
@Validated
@Api(tags="司机管理")
@RequestMapping(value = "/drivers")
public class DriverController extends BaseController {

    @Autowired
    private DriverClient driverClient;
    @Autowired
    private CompanyClient companyClient;
    @Autowired
    private TruckClient truckClient;

    @GetMapping
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取企业列表", notes = "获取企业列表")
    public PageResult<DriverListVO> find(@ApiParam(value = "查询参数")  DriverFilterParam filterParam,
                                         @ApiParam(value = "分页查询参数") RequestFilterParams params) {

        return driverClient.find(JSONResultUtil.object2JSON(filterParam), JSONResultUtil.object2JSON(params));
    }

    @GetMapping("/{id}")
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取司机详细", notes = "获取司机详细")
    public DriverVO getById(@PathVariable("id") Integer id) {
        DriverVO driverVO = driverClient.getById(id, Global.ZERO);
        List<CompanyDriverVO> companyDriverVOs = companyClient.getByDriverIdAndCompanyId(id, Global.ZERO);
        if (CollectionUtils.isNotEmpty(companyDriverVOs)){
            companyDriverVOs.forEach(companyDriverVO->{
                companyDriverVO.setCompanyName(companyClient.getById(companyDriverVO.getCompanyId()).getName());
                if (Objects.nonNull(companyDriverVO.getTruckId())){
                    TruckVO truckVO = truckClient.getById(companyDriverVO.getTruckId(), Global.ZERO, Global.ZERO);
                    companyDriverVO.setTruckNumber(truckVO.getHeaderLicenseVO().getLicence());
                }
            });
            driverVO.setCompanyDrivers(companyDriverVOs);
        }

        return driverVO;
    }



    @PutMapping("/{id}")
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "修改司机信息", notes = "修改司机信息")
    public void update(@PathVariable("id") Integer id,@RequestBody @Valid DriverCommand command) {
        driverClient.update(command,id,super.getUserId());
    }


}
